#ifdef _WIN32
#pragma warning(disable:4996)
#include <iostream>
#include <WinSock2.h>
#include <cstring>

#pragma comment(lib,"ws2_32.lib")

using namespace std;

string serverip = "113.45.61.226";
uint16_t serverport = 8080;

int main()
{

    WSAData wsd;
    //启动Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) {/*进行WinSocket的初始化,
        windows 初始化socket网络库，申请2，2的版本，windows socket编程必须先初始化。*/
        cout << "WSAStartup Error = " << WSAGetLastError() << endl;
        return 0;
    }
    else {
        cout << "WSAStartup Success" << endl;
    }
    
    //下面就一样了
    SOCKET csock = socket(AF_INET, SOCK_DGRAM, 0);
    if (csock == SOCKET_ERROR) {
        cout << "socket Error = " << WSAGetLastError() << endl;
        return 1;
    }
    else {
        cout << "socket Success" << endl;
    }
   
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

#define NUM 1024
    char buffer[NUM];
    char inbuffer[NUM];
    while (true)
    {
        // 发送逻辑
        cout << "Please Enter# ";
        fgets(buffer, sizeof(buffer), stdin);
        int n = sendto(csock, buffer, sizeof(buffer), 0,(struct sockaddr*)&server, sizeof(server));
        if (n < 0)
        {
            cerr << "sendto err!" << endl;
            break;
        }

        struct sockaddr_in peer;
        int peerlen = sizeof(peer);
        // 收到数据
        inbuffer[0] = 0;// C风格的清空
        n = recvfrom(csock,inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&peer, &peerlen);
        if (n > 0)
        {
            inbuffer[n] = 0;
            cout << "server 返回的消息是# " << inbuffer << endl;
        }
        else break;
    }
    closesocket(csock);
    WSACleanup();
	return 0;
}
#endif